package com.example.fouroperations;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AnswerActivity extends AppCompatActivity{

    public static final String TAG ="AnswerActivity";
    int countdown;
    int quesNum;
    int count;
    float preScore;
    float totalScore;
    int rightQuesNum;
    int totalTime;
    ArrayList<String> ques_list;
    ArrayList<String> ans_list;

    TextView quesID;
    TextView question;
    TextView answerTitle;
    TextView rightAnswer;
    TextView countdownText;
    TextView resultText;
    EditText inputAnswer;

    Button submitBtn;
    Button nextBtn;
    Button finishBtn;
    Handler handler;
    long time;
    CountDownTimer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);

        //从主页面获取数据
        Intent answerIntent = getIntent();
        Bundle answerBdl = answerIntent.getExtras();
        ques_list = answerBdl.getStringArrayList("ques_list");
        ans_list = answerBdl.getStringArrayList("ans_list");
        quesNum = answerBdl.getInt("ques_num",0);
        countdown = answerBdl.getInt("countdown",0);
        preScore = answerBdl.getFloat("preScore",0.0f);

        Log.i(TAG, "onCreate: countdown="+countdown);
        Log.i(TAG, "onCreate: ques_num="+ quesNum);
        Log.i(TAG, "onCreate:  preScore= "+preScore);

        countdownText = findViewById(R.id.timer);//还没有想好如何实现——倒计时
        quesID = findViewById(R.id.quesId);
        question = findViewById(R.id.question);
        answerTitle = findViewById(R.id.answerTitle);
        rightAnswer = findViewById(R.id.rightAnswer);
        resultText = findViewById(R.id.resultText);
        submitBtn = findViewById(R.id.submitBtn);
        nextBtn = findViewById(R.id.nextBtn);
        finishBtn = findViewById(R.id.finishBtn);

        //初始化分数、正确题数、答题总耗时
        totalScore = 0.0f;
        rightQuesNum = 0;
        totalTime = 0;

        //设置题号、倒计时、问题、答案
        count = 1;
        quesID.setText(count+"/"+quesNum);
        question.setText(ques_list.get(count-1));
        if(ans_list.get(count-1).equals("-0.00"))
            rightAnswer.setText("0.00");
        else
            rightAnswer.setText(ans_list.get(count-1));

        time=Long.parseLong(String.valueOf(countdown+1));
        timer = new CountDownTimer(1000 * time, 1000) {
            @SuppressLint("DefaultLocale")
            @Override
            public void onTick(long millisUntilFinished) {
                countdownText.setText(String.format("%d",millisUntilFinished/1000));
                totalTime++;
            }

            @Override
            public void onFinish() {
                countdownText.setText(R.string.timeTip);
                //时间已到，自动触发submitClick函数
                submitBtn.performClick();
                //倒计时结束显示
                Toast.makeText(AnswerActivity.this, R.string.timeTip, Toast.LENGTH_SHORT).show();
            }
        };
        timer.start();

    }

    //点击"submit"——用户提交答案
    public void submitClick(View btn) {
        Log.i(TAG, "submitClick: ");
        timer.cancel();
        //获取用户输入答案
        inputAnswer = findViewById(R.id.inputAnswer);
        String answer_temp = inputAnswer.getText().toString();
        Log.i(TAG, "submitClick: inputString="+answer_temp);
        if(answer_temp.equals(""))
        {
            resultText.setText(R.string.wrongText);
        }
        else{
            float answer = Float.parseFloat(answer_temp);

            //匹配答案
            float rightAnswer_f = Float.parseFloat(ans_list.get(count-1));
            if(answer == rightAnswer_f){
                //加分
                totalScore += preScore;
                rightQuesNum += 1;
                resultText.setText(R.string.rightText);
            }else{
                resultText.setText(R.string.wrongText);
            }
        }

        //设置可见不可见
        resultText.setVisibility(View.VISIBLE);
        answerTitle.setVisibility(View.VISIBLE);
        rightAnswer.setVisibility(View.VISIBLE);

        submitBtn.setVisibility(View.INVISIBLE);
        if(count<quesNum)
            nextBtn.setVisibility(View.VISIBLE);
        else
            finishBtn.setVisibility(View.VISIBLE);
    }

    //点击"next"——已提交答案，点击下一题
    public void nextClick(View btn) {
        Log.i(TAG, "nextClick: ");
        timer.start();
        //设置题号、倒计时、问题、答案
        count += 1;
        quesID.setText(count+"/"+quesNum);
        question.setText(ques_list.get(count-1));
        if(ans_list.get(count-1).equals("-0.00"))
            rightAnswer.setText("0.00");
        else
            rightAnswer.setText(ans_list.get(count-1));

        //将用户输入答案改为空
        inputAnswer = findViewById(R.id.inputAnswer);
        inputAnswer.setText("");

        //设置可见不可见
        resultText.setVisibility(View.INVISIBLE);
        answerTitle.setVisibility(View.INVISIBLE);
        rightAnswer.setVisibility(View.INVISIBLE);

        submitBtn.setVisibility(View.VISIBLE);
        nextBtn.setVisibility(View.INVISIBLE);
        finishBtn.setVisibility(View.INVISIBLE);
    }

    //点击"finish"——结束答题，跳转最后页面
    public void finishClick(View btn) {
        Log.i(TAG, "finishClick: ");


        //使用Buddle打包发送数据(总分、答对题目数、总题数、总时间)
        Intent ansToResIntent = new Intent(this, com.example.fouroperations.ResultActivity.class);

        Bundle ansToResBdl = new Bundle();
        ansToResBdl.putFloat("totalScore", totalScore);
        ansToResBdl.putInt("ques_num", quesNum);
        ansToResBdl.putInt("rightQuesNum", rightQuesNum);
        ansToResBdl.putInt("totalTime", totalTime);
        ansToResBdl.putFloat("preScore",preScore);
        ansToResIntent.putExtras(ansToResBdl);

        Log.i(TAG, "finishClick: preScore="+preScore);
        Log.i(TAG, "finishClick: totalScore="+totalScore);
//        startActivityForResult(mainIntent, 1);
        startActivity(ansToResIntent);

    }

}